package com.example.localareanetworkchat.fragment.home.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.localareanetworkchat.R
import com.example.localareanetworkchat.persistence.entity.ChatGroupUser

class ChatGroupMemberListAdapter(val context: Context): ListAdapter<ChatGroupUser, ChatGroupMemberListAdapter.MyViewHolder>(object :
    DiffUtil.ItemCallback<ChatGroupUser>() {
    override fun areItemsTheSame(oldItem: ChatGroupUser, newItem: ChatGroupUser): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: ChatGroupUser, newItem: ChatGroupUser): Boolean {
        return oldItem == newItem
    }

})
{

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvMemberNameFirst: TextView = itemView.findViewById(R.id.tv_chat_group_info_member_item_name_first)
        val tvMemberName: TextView = itemView.findViewById(R.id.tv_chat_group_info_member_item_name)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder = MyViewHolder(LayoutInflater.from(context).inflate(R.layout.chat_group_member_item, parent, false))

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.tvMemberNameFirst.text = getItem(position).userName.first().toString()
        holder.tvMemberName.text = getItem(position).userName
    }

}